package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.domain.enumclass.ProductClientType;
import com.rockcent.mall.lib.domain.enumclass.ProductStatus;
import com.rockcent.mall.lib.domain.enumclass.ProductType;
import com.rockcent.mall.lib.dto.ProductDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

/**
 * Created by Marco on 16-11-14.
 * 产品管理
 */
@Repository
public class MallProductRepositoryImpl implements MallProductRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallProductRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    /**
     * 产品管理查询
     * @param mallId
     * @param name
     * @param id
     * @param type
     * @param status
     * @param merchantName
     * @param beginDate
     * @param endDate
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<ProductDto> findMerchantProductQuery(Long mallId, String name, Long id, String type, String status, String merchantName, String beginDate, String endDate, PageResult pageResult) {
        SQLPageDoing<ProductDto> doing = (sql, fields, params, typeReferenceMap) -> {
            if(StringUtils.isNotBlank(merchantName)) {
                sql.append("select %s from product p left join merchant m on p.merchant_id = m.id where 1=1 AND m.name LIKE :merchantName");
                params.put("merchantName", "%" + merchantName + "%");
            } else {
                sql.append("SELECT %s FROM product p WHERE 1=1 ");
            }
            if(mallId!=null) {
                sql.append(" AND p.mall_id = :mallId");
                params.put("mallId", mallId);
            }
            //产品名称
            if (StringUtils.isNotBlank(name)) {
                sql.append(" AND p.name LIKE :name");
                params.put("name", "%" + name + "%");
            }
            //产品编号
            if (id!=null) {
                sql.append(" AND p.id = :id");
                params.put("id", id);
            }
            //产品类型
            if (StringUtils.isNotBlank(type) && ProductType.ALL != ProductType.valueOf(type)) {
                sql.append(" AND p.type =:type");
                params.put("type", type);
            }
            //产品状态
            if (StringUtils.isNotBlank(status)) {
                if(com.rockcent.mall.lib.domain.enumclass.ProductStatus.valueOf(status) == com.rockcent.mall.lib.domain.enumclass.ProductStatus.WAIT_AUDIT){
                    sql.append(" AND p.status in (:status) ");
                    params.put("status", Arrays.asList(com.rockcent.mall.lib.domain.enumclass.ProductStatus.AUDIT_FAIL.toString(), com.rockcent.mall.lib.domain.enumclass.ProductStatus.WAIT_AUDIT.toString()));
                } else {
                    sql.append(" AND p.status = :status");
                    params.put("status", status);
                }
            }
            //上架开始时间
            if (StringUtils.isNotBlank(beginDate)) {
                sql.append(" AND p.date_selling >= :beginDate");
                params.put("beginDate", beginDate);
            }
            //上架结束时间
            if (StringUtils.isNotBlank(endDate)) {
                sql.append(" AND p.date_selling <= :endDate");
                params.put("endDate", endDate);
            }
            sql.append(" order by p.date_created desc ");
            fields.append("p.*,(select c.name from product_categories pc,category c where pc.category_id=c.id and pc.product_id=p.id limit 1) categoryName,(select sum(moi.buy_count) from merchant_order_item moi where moi.product_id=p.id limit 1) buyNum,(select sum(sr.settle_num) from settlement_record sr where sr.product_id=p.id limit 1) settleNum");
            if(StringUtils.isNotBlank(merchantName)) {
                fields.append(", m.name merchantName");
            } else {
                fields.append(", (select mt.name from merchant mt where mt.id = p.merchant_id limit 1) merchantName");
            }
        };
        return doing.go(pageResult, ProductDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     * C端精选产品的首页列表
     * @param mallId
     * @param productClientType
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<Product> findMiniExplosionProducts(Long mallId, ProductClientType productClientType, PageResult pageResult) {
        SQLPageDoing<Product> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM product p WHERE 1=1 ");
            sql.append(" AND p.mall_id = :mallId");
            params.put("mallId", mallId);
            sql.append(" AND p.product_client_type = :productClientType");
            sql.append(" AND p.status = 'SELLING'");
            params.put("productClientType", String.valueOf(productClientType));
            sql.append(" order by p.sort_num asc ");
            fields.append("p.*");
        };
        return doing.go(pageResult, Product.class, objectMapper, jdbcTemplate, log);
    }
}
